package com.baiqian.doupo.block.yaocai;

import com.baiqian.doupo.item.ItemRegistry;
import net.minecraft.block.BlockState;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;

//洗骨花
public class XiGuHua extends YaoCai {
    public XiGuHua() {
        super();
    }

    //破坏收割的方式给少量种子，大量作物
    @Override
    public void onBlockHarvested(World worldIn, BlockPos pos, BlockState state, PlayerEntity player) {
        super.onBlockHarvested(worldIn,pos,state,player);
        if(!worldIn.isRemote&&!canGrow(worldIn,pos,state,worldIn.isRemote)) {
            ItemStack itemStack = new ItemStack(ItemRegistry.xi_gu_hua.get());
            itemStack.setCount((int) (2 + Math.random()*3));
            ItemStack itemStack2 = new ItemStack(ItemRegistry.xi_gu_hua_seeds.get());
            itemStack2.setCount(1);
            worldIn.addEntity(new ItemEntity(worldIn, pos.getX(), pos.getY(), pos.getZ(), itemStack));
            worldIn.addEntity(new ItemEntity(worldIn, pos.getX(), pos.getY(), pos.getZ(), itemStack2));
        }
    }

    protected IItemProvider getSeedsItem() {
        return ItemRegistry.xi_gu_hua_seeds.get();
    }

    public ItemStack getItem(IBlockReader worldIn, BlockPos pos, BlockState state) {
        return new ItemStack(this.getSeedsItem());
    }

    //右键收割的方式给大量种子，少量作物
    public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
        int i = state.get(AGE);
        boolean flag = i == 7;
        if (!flag && player.getHeldItem(handIn).getItem() == Items.BONE_MEAL) {
            return ActionResultType.PASS;
        } else if (flag) {
            spawnAsEntity(worldIn, pos, new ItemStack(ItemRegistry.xi_gu_hua.get(), 1));
            spawnAsEntity(worldIn, pos, new ItemStack(ItemRegistry.xi_gu_hua_seeds.get(), (int) (2 + Math.random()*3)));
            worldIn.playSound((PlayerEntity)null, pos, SoundEvents.ITEM_SWEET_BERRIES_PICK_FROM_BUSH, SoundCategory.BLOCKS, 1.0F, 0.8F + worldIn.rand.nextFloat() * 0.4F);
            worldIn.setBlockState(pos, state.with(AGE, Integer.valueOf(0)), 2);
            return ActionResultType.func_233537_a_(worldIn.isRemote);
        } else {
            return super.onBlockActivated(state, worldIn, pos, player, handIn, hit);
        }
    }
}
